window.addEventListener("load", () => {
    //获取
    const openInformation = document.querySelector(".open_brief_information")
    const closeInformation = document.querySelector(".close_brief_information")
    const Information = document.querySelector(".detail_right_information")

    //打开功能
    openInformation.addEventListener("click", () => {
        Information.style.display = "block"
        // information()
    })
    //关闭
    closeInformation.addEventListener("click", () => {
        Information.style.display = "none"
    })
    collectItem()
})



let data
let episodes
let episodeNum = 0
let token = localStorage.getItem("token")
let playId = localStorage.getItem('playId')
let playType = localStorage.getItem('playType')
const video = document.querySelector("video")
window.onload = function () {
    information()
    if (token) {
        document.querySelector(".user_icon").src = "images/user_login_icon.png"
    }
    const xhr = new XMLHttpRequest()
    xhr.responseType = 'json'
    xhr.open('GET', `https://blog.zifeiyu.love/video/queryEpisode?id=${playId}`)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                data = xhr.response.data
                changeVideo(0)
                if (playType === '电视剧') {
                    // console.log(data.length)
                    document.querySelector(".play-introduction-renewtime").style.display = "block"
                    document.querySelector(".play-list-number").style.display = "block"
                    document.querySelector(".renew_information").style.display = "block"
                    episodes = data.length
                    localStorage.setItem('playData', JSON.stringify(data))
                    for (let i = 2; i <= episodes; i++) {
                        addEpisode(i)
                    }
                }
                else if (playType === '电影') {
                    document.querySelector(".renew").style.display = "none"
                    document.querySelector(".play_list_movie").style.display = "block"
                }
            }
        }
    }
}

function changeVideo(episodeNum) {
    video.src = data[episodeNum].url
    video.play()
}

// 复制集数节点
function addEpisode(i) {
    const episodeNode = document.querySelector(".play-list_item")
    const cloneEpisode = episodeNode.cloneNode(true)
    cloneEpisode.innerHTML = i
    document.querySelector(".play-list-number-button").appendChild(cloneEpisode)
}

// 点击更换视频
function clickEpisode() {
    const episodeButton = document.querySelectorAll(".play-list_item")
    episodeButton.forEach((item, number) => {
        item.addEventListener("click", function () {
            changeVideo(number)
        })
    })
}


//更换简介内容
function information() {
    let dataInfo
    const xhr = new XMLHttpRequest()
    xhr.responseType = 'json'
    xhr.open('GET', `https://blog.zifeiyu.love/video/query?id=${playId}`)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                dataInfo = xhr.response.data
                // console.log(dataInfo)
                document.title = dataInfo.name
                document.querySelector(".play_name").innerHTML = dataInfo.name
                document.querySelector(".play_title").innerHTML = dataInfo.name
                document.querySelector(".play_list_movie_content_title").innerHTML = dataInfo.name
                document.querySelector(".cover").src = dataInfo.cover
                document.querySelector(".play_list_movie_content_cover img").src = dataInfo.cover
                // document.querySelector(".close_brief_information span").innerHTML = dataInfo.name
                document.querySelector(".hotpot").innerHTML = dataInfo.hotpot
                document.querySelector(".release_place").innerHTML = ` · ${dataInfo.releasePlace}`
                // document.querySelector(".release_time").innerHTML = ` · ${dataInfo.releaseTime.toString().substring(0, 4)}`
                document.querySelector(".release_time").innerHTML = ` · ${dataInfo.releaseTime.toString().substring(0, 10)}`
                document.querySelector(".tag1").style.display = "none"
                document.querySelector(".tag2").innerHTML = dataInfo.tag
                // < span class="information_tags" > 16134 · 内地 · 2023 · 东方玄幻 · 玄幻修真 · 英雄成长 · 热血战斗</ >
                document.querySelector(".information_tags").innerHTML = `${dataInfo.hotpot} · ${dataInfo.releasePlace} · ${dataInfo.releaseTime.toString().substring(0, 10)} · ${dataInfo.tag}`
                document.querySelector(".renew").innerHTML = `更新至${dataInfo.nowCount}集 · 全${dataInfo.allCount}集`
                document.querySelector(".renew_information").innerHTML = `更新至${dataInfo.nowCount}集 · 全${dataInfo.allCount}集`
                document.querySelector(".daily").innerHTML = dataInfo.daily
                document.querySelector(".information_below_content").innerHTML = dataInfo.introduction
                // document.querySelector("cover").src = dataInfo.cover
                // document.querySelector("cover").src = dataInfo.cover

                // // console.log(data.length)
                // episodes = data.length
                // localStorage.setItem('playData', JSON.stringify(data))
                // // console.log(data)
                // // console.log(data[0].url)
                // // video.src = data[0].url
                // // video.play()
                // for (let i = 2; i <= episodes; i++) {
                //     addEpisode(i)
                // }
                // changeVideo(0)
            }
        }
    }

}

// 收藏
let collected = 0
let getStorageListAll = JSON.parse(localStorage.getItem('storageDataAll'))
function collectItem() {
    document.querySelector(".collect").addEventListener("click", function () {
        if (token) {
            const xhr = new XMLHttpRequest()
            xhr.responseType = 'json'
            xhr.open('GET', `https://blog.zifeiyu.love/video/star?id=${playId}`)
            xhr.setRequestHeader("token", `${token}`)
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        const collectBtn = document.querySelector(".collect")
                        if (collected) {
                            collectBtn.src = "images/add-icon.png"
                            collected = 0
                        }
                        else {
                            collectBtn.src = "images/collect_already_detail.png"
                            collected = 1
                        }
                        // console.log(xhr.response.data)
                    } else {
                        console.error('Request failed:' + xhr.status)
                    }
                }
            }
        }
        else {
            alert('未登录')
        }
        console.log(num)
    })
}

document.querySelector("video").addEventListener("play", function () {
    const xhr = new XMLHttpRequest()
    xhr.responseType = 'json'
    xhr.open('GET', `https://blog.zifeiyu.love/video/join?id=${playId}&time=00:00:05`)
    xhr.setRequestHeader("token", `${token}`)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                // alert('历史记录成功')
            } else {
                console.error('Request failed:' + xhr.status)
            }
        }
    }
})